package com.eugene.ui.views;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.views.properties.IPropertySheetPage;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.eugene.element.CurveBucket;
import com.eugene.entity.Scenario;
import com.eugene.enums.EMarketVariableType;
import com.eugene.ui.provider.MarketVariableLabel;
import com.eugene.ui.provider.MarketVariableContent;
import com.eugene.ui.provider.ScenarioLabel;
import com.eugene.ui.provider.ScenarioContent;

public class MarketVariableView extends ViewPart implements ITabbedPropertySheetPageContributor{
//	private TableViewer tableViewer;
	
	private TreeViewer treeViewer;
	private Composite  myControl;
	
	public void createPartControl(Composite parent) {
		
		treeViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL| SWT.V_SCROLL);
//		treeViewer = new CheckboxTreeViewer(parent, SWT.MULTI | SWT.H_SCROLL| SWT.V_SCROLL);
		treeViewer.setContentProvider(new MarketVariableContent());
		treeViewer.setLabelProvider(new MarketVariableLabel());
// Expand the tree 
		treeViewer.setAutoExpandLevel(1);
// Provide the input to the ContentProvider, every class except null		
		treeViewer.setInput(EMarketVariableType.values());
					
// filter Setting
//		treeViewer.setFilters(new ViewerFilter[]{new HistoricalScenarioFilter()});
// Tree Background color
//		treeViewer.getTree().setBackground(blue);
					
//Selection Provider
		getSite().setSelectionProvider(treeViewer);
		System.out.println("Test " + getSite().getId());
		hookDoubleClickCommand();
					
//		createFiltersAndSorters();
//		createActions();
//		createMenus();
//		createToolbar(); 
//		createContextMenu();	
						
	}
	@Override
	public void setFocus() {
		treeViewer.getControl().setFocus();
	}
	//For Tabbed Property  	
  public Object getAdapter(Class adapter) {
      if (adapter == IPropertySheetPage.class)
          return new TabbedPropertySheetPage(this);
      return super.getAdapter(adapter);
  }
  @Override
  public String getContributorId(){
	  return getSite().getId();
  }
  
  private void hookDoubleClickCommand() {
	  treeViewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				IHandlerService handlerService = (IHandlerService)getSite().getService(IHandlerService.class);
				System.out.println("Arrive At Service");
				try {
					System.out.println("Test 19 : before the Execution");
					handlerService.executeCommand(
							"com.eugene.ui.commands.CallIrCurveEditor", null);
				} catch (Exception ex) {
//					throw new RuntimeException("CallIrCurveEditor not found");
//					MessageDialog.openWarning(getSite().getShell(),"wrong","no");
					
				}
			}
		});
	}


}
